#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_err.h"

// 引脚
#define GPIO_PIN_R  5
#define GPIO_PIN_G  19
#define GPIO_PIN_B  21
#define GPIO_PIN_R_SEL  (1ULL<<GPIO_PIN_R)
#define GPIO_PIN_G_SEL  (1ULL<<GPIO_PIN_G)
#define GPIO_PIN_B_SEL  (1ULL<<GPIO_PIN_B)

// lcd控制器
#define LEDC_HS_TIMER          LEDC_TIMER_0
#define LEDC_HS_MODE           LEDC_HIGH_SPEED_MODE

#define LEDC_HS_CH0_GPIO       GPIO_PIN_R
#define LEDC_HS_CH0_CHANNEL    LEDC_CHANNEL_0
#define LEDC_HS_CH1_GPIO       GPIO_PIN_G
#define LEDC_HS_CH1_CHANNEL    LEDC_CHANNEL_1
#define LEDC_HS_CH2_GPIO       GPIO_PIN_B
#define LEDC_HS_CH2_CHANNEL    LEDC_CHANNEL_2

#define LEDC_CH_NUM       3

/*
	* Prepare individual configuration
	* for each channel of LED Controller
	* by selecting:
	* - controller's channel number
	* - output duty cycle, set initially to 0
	* - GPIO number where LED is connected to
	* - speed mode, either high or low
	* - timer servicing selected channel
	*   Note: if different channels use one timer,
	*         then frequency and bit_num of these channels
	*         will be the same
	*/
const ledc_channel_config_t ledc_channel[LEDC_CH_NUM] = {
	{
		.channel    = LEDC_HS_CH0_CHANNEL,
		.duty       = 0,
		.gpio_num   = LEDC_HS_CH0_GPIO,
		.speed_mode = LEDC_HS_MODE,
		.hpoint     = 0,
		.timer_sel  = LEDC_HS_TIMER
	},
	{
		.channel    = LEDC_HS_CH1_CHANNEL,
		.duty       = 0,
		.gpio_num   = LEDC_HS_CH1_GPIO,
		.speed_mode = LEDC_HS_MODE,
		.hpoint     = 0,
		.timer_sel  = LEDC_HS_TIMER
	},
	{
		.channel    = LEDC_HS_CH2_CHANNEL,
		.duty       = 0,
		.gpio_num   = LEDC_HS_CH2_GPIO,
		.speed_mode = LEDC_HS_MODE,
		.hpoint     = 0,
		.timer_sel  = LEDC_HS_TIMER
	}
};

static unsigned long _color = 0x00000000;

// 设置颜色
void led_set_color(unsigned long color)
{
	int ch;
	_color = color;
	for (ch = 0; ch < LEDC_CH_NUM; ch++) {
		// 0x0000 ~ 0x1FFF
		// 0x1F太亮了,改为0x0A
		ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0x0A * (color>>ch*8 & 0xFF));
		ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
	}
}

// 初始化
void led_init() {
	int ch;
    // 控制器配置
    ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
        .freq_hz = 5000,                      // frequency of PWM signal
        .speed_mode = LEDC_HS_MODE,           // timer mode
        .timer_num = LEDC_HS_TIMER,            // timer index
        .clk_cfg = LEDC_AUTO_CLK,              // Auto select the source clock
    };
    // Set configuration of timer0 for high speed channels
    ledc_timer_config(&ledc_timer);

	// Set LED Controller with previously prepared configuration
    for (ch = 0; ch < LEDC_CH_NUM; ch++) {
        ledc_channel_config(&ledc_channel[ch]);
    }

	/*
	// Initialize fade service.
    ledc_fade_func_install(0);
	//设置渐变时间
	for (ch = 0; ch < LEDC_CH_NUM; ch++) {
		ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
				ledc_channel[ch].channel, LEDC_DUTY, LEDC_FADE_TIME);
		ledc_fade_start(ledc_channel[ch].speed_mode,
				ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
	}
	*/
}

// 获取颜色值
unsigned long  led_get_color(void) {
	return _color;
}
